package com.xaicode.template.gendata.mall;

import com.alibaba.fastjson.JSON;
import com.xaicode.template.gendata.base.LogUtil;
import com.xaicode.template.gendata.common.ConfigUtil;
import com.xaicode.template.gendata.common.ParamUtil;
import com.xaicode.template.gendata.mall.common.RandomNum;
import com.xaicode.template.gendata.mall.common.RandomOptionGroup;
import com.xaicode.template.gendata.mall.config.AppConfig;
import com.xaicode.template.gendata.mall.enums.PageId;
import com.xaicode.template.gendata.mall.model.AppAction;
import com.xaicode.template.gendata.mall.model.AppDisplay;
import com.xaicode.template.gendata.mall.model.AppMain;
import com.xaicode.template.gendata.mall.model.AppPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.EnumUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Locker xaicode@sina.com
 * @version 1.0
 * @date 2022/5/24 0024 22:22
 */
@Slf4j
@Component
public class Mocker implements Runnable {

    private final String PATH_JSON = "mall/path.json";

    private Long ts;

    public List<AppMain> doAppMock() {
        List<AppMain> logList = new ArrayList<>();
        this.ts = ParamUtil.checkDate(AppConfig.mock_date).getTime();

        // 构建访问路径
        String jsonFile = ConfigUtil.loadJsonFile(PATH_JSON);
        List<Map> pathList = JSON.parseArray(jsonFile, Map.class);
        RandomOptionGroup.Builder<List> builder = RandomOptionGroup.builder();
        for (Map map : pathList) {
            List path = (List) map.get("path");
            Integer rate = (Integer) map.get("rate");
            builder.add(path, rate);
        }
        List chosenPath = builder.build().getRandomOpt().getValue();

        // 遍历访问路径
        PageId lastPageId = null;
        for (Object o : chosenPath) {
            AppMain pageBuilder = new AppMain();

            // 模拟页面停留时间
            int pageDuringTime = RandomNum.getRandInt(1000, AppConfig.page_during_max_ms);

            PageId pageId = (PageId) EnumUtils.getEnum(PageId.class, (String) o);
            AppPage page = AppPage.build(pageId, lastPageId, pageDuringTime);
            if (pageId == null) {
                System.out.println();
            }
            pageBuilder.setPage(page);

            lastPageId = page.getPage_id();

            List<AppAction> appActionList = AppAction.buildList(page, this.ts, pageDuringTime);
            if (appActionList.size() > 0) {
                pageBuilder.setActions(appActionList);
            }

            List<AppDisplay> displayList = AppDisplay.buildList(page);
            if (displayList.size() > 0) {
                pageBuilder.setDisplays(displayList);
            }
            pageBuilder.setTs(this.ts);

            logList.add(pageBuilder);
            this.ts = this.ts + 1000L;
        }

        return logList;
    }

    public void run() {
        List<AppMain> appMainList = doAppMock();

        for (AppMain appMain : appMainList) {

            if (AppConfig.mock_type.equals("log")) {
                LogUtil.log(appMain.toString());
            } else {
                log.warn("wait for more app type");
            }

            try {
                Thread.sleep(AppConfig.log_sleep);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
